package com.vast.vastlcd.lcdAPI;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 
 */
public class LCDSetPixelsRequest {
	public LCDMessageHeader lcdMessageHeader;
	public int nX;
	public int nY;
	public int nLength;
	public int nRed;
	public int nGreen;
	public int nBlue;
	public int nOperation;

	/**
	 * @return
	 */
	public byte[] javaToNative () {
	 	if (this == null) 
	 		return null;
	 	
	 	byte[] buffer = null;
	 	try {
			// write data to a byte array and then ask super to convert to pMedium
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			LCDDataOutputStream writeOut = new LCDDataOutputStream(out);
			
			writeOut.write(lcdMessageHeader.javaToNative());
			
			writeOut.writeLSBInt(nX);
			writeOut.writeLSBInt(nY);
			writeOut.writeLSBInt(nLength);
			writeOut.writeLSBInt(nRed);
			writeOut.writeLSBInt(nGreen);
			writeOut.writeLSBInt(nBlue);
			writeOut.writeLSBInt(nOperation);
			
			buffer = out.toByteArray();
			writeOut.close();			
			
		} catch (IOException e) {

		}
		return buffer;
	}
	
	/**
	 * @param buffer
	 * @return
	 */
	static public LCDSetPixelsRequest nativeToJava(byte[] buffer){	
	 
 		if (buffer == null) 
 			return null;
 		
 		LCDSetPixelsRequest spr = new LCDSetPixelsRequest();
 		try {
 			ByteArrayInputStream in = new ByteArrayInputStream(buffer);
 			LCDDataInputStream readIn = new LCDDataInputStream(in); 			
 			 
 			spr.nX = readIn.readLSBInt();
 			spr.nY = readIn.readLSBInt();
 			spr.nLength = readIn.readLSBInt();
 			spr.nRed = readIn.readLSBInt();
 			spr.nGreen = readIn.readLSBInt();
 			spr.nBlue = readIn.readLSBInt();
 			spr.nOperation = readIn.readLSBInt();
 			
 			readIn.close();
 		} catch (IOException ex) {
 			return null;
 		}
 		
 		return spr; 		
	}
	 
	/**
	 * @param msgBodyBuffer
	 */
	static void processMessage(byte[] msgBodyBuffer) {
		 LCDSetPixelsRequest spr = nativeToJava(msgBodyBuffer);
		 
		 spr.updateViewer();
		 
	}
	
	/**
	 * 
	 */
	void updateViewer() {
		 /*IWorkbench wb = PlatformUI.getWorkbench();	
		 ImageLoader newImage = new ImageLoader(); 
		 
		 ByteArrayInputStream in = new ByteArrayInputStream(imageData);
		 
		 ImageData[] image = newImage.load(in);
		 
		 //wb.getActiveWorkbenchWindow().getActivePage().
		 
		 Display currDevice = Display.getCurrent();
		 Image blah = new Image(currDevice, in);
		
		 // update size of the window*/
	 }
}
